<?php

// AudioShout 0.1.0 Alpha

// Global application functions. Do not modify anything below this line unless you know what you are doing.

$filename = urlencode($_FILES['file']['name']);
$filesize = urlencode($_FILES['file']['size']);
$filehash = urlencode(hash_file('sha1',$_FILES['file']['tmp_name'],false));
$title = $_POST['title']; 
$ip = urlencode($_SERVER['REMOTE_ADDR']);
$agent = urlencode($_SERVER['HTTP_USER_AGENT']);

function dbconnect()
// Connects to the MySQL database using MySQLi.
{
global $dbhost, $dbuser, $dbpass, $dbname;
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if (!$mysqli->connect_error)
{
	return $mysqli;
} else {
	die('Error: We could not connect to the database. Please report the following error to the site administrator: (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
}

function dbresult($statement, $row, $field)
// Returns the results of a MySQLi query. Usage is the same as the old mysql_result() function.
{
global $statement;
  if ($statement->num_rows==0) return 'unknown'; 
  $statement->data_seek($row);
  $array=$result->fetch_assoc(); 
  $result=$array[$field]; 
  return $result; 
}

function getid($SQLMaster,$servername,$filename,$filesize,$filehash,$ip,$agent,$title)
// Retrieves the ID generated by the last file upload.
{
    global $SQLMaster, $servername, $domain, $filename, $filesize, $filehash, $ip, $agent, $title;
    $ID = intval(file_get_contents("http://$SQLMaster.$domain/api/upload.php?a=u&s=$servername&fn=$filename&fs=$filesize&fh=$filehash&ip=$ip&ua=$agent&ft=$title"));
return $ID;
}

function checkid($ID) 
// Checks to make sure that a file with the given ID does not already exist on the local node.
{
global $multiserver, $servername, $SQLMaster, $uploadsdir, $ID;
		if (file_exists($uploadsdir.$ID))
		  {
		  echo ".$ID. already exists.";
		  }
		else
		  {
		move_uploaded_file($_FILES['file']['tmp_name'],$uploadsDir.$ID);
		if ($multiserver == true)
			{
			file_get_contents("http://$SQLMaster.$domain/api/upload.php?a=s&s=$servername&id=$ID");
			}
}
}

function getfile($url,$df)
/* Download a file from the specified URL and store it in a file on the local node.
   Parameters: url=URL to download, df=Destination file
*/
{
$ch = curl_init($url);
$fp = fopen($df, 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
return true;
}

function verifyextension($filename)/* Check the extension of a file
   Parameter: filename=filename to check
   Return value: true if extension is valid, false if not
*/
{
global $filename;
$ext = pathinfo($filename,PATHINFO_EXTENSION);
$ext = strtolower($ext);
$allowedexts = array('mp3','ogg','wav','m4a','flac','aiff','aac','mid');
return in_array($ext,$allowedexts);
}

function fileinfo($ID)
// Returns information about a file based on the given ID.
{
global $ID;
$query = "SELECT Filename,Filehash,Date,Filesize,Title FROM uploads WHERE ID=?";
	$mysqli = dbconnect();
$statement = $mysqli->stmt_init();
if ($statement->prepare($query))
		{
		$statement->bind_param("i", $ID);
		$statement->execute();
		$statement->store_result();
		$statement->bind_result($filename, $filehash, $date, $filesize, $title);
		$statement->fetch();
$numrows = $statement->num_rows;
	$statement->close();
	$mysqli->close();
} else {
echo "Could not prepare the statement. Variables: " . $dbhost, $dbuser, $dbpass, $dbname . $mysqli->error;
$statement->close();
$mysqli->close();
}
return array($filename,$filehash,$filedate,$filesize,$title);
}
